/**
 * 
 */
package com.uniandes.modelviewer.tools;

import java.io.File;
import java.util.ArrayList;

import com.uniandes.modelviewer.Attribute;
import com.uniandes.modelviewer.Class;
import com.uniandes.modelviewer.Model;
import com.uniandes.modelviewer.Relation;
import com.uniandes.modelviewer.converter.ModelReader;
import com.uniandes.modelviewer.dsl.XmiModel;

/**
 * @author User
 *
 */
public class CustomUpload {
	
	private ModelReader reader;
	
	public Model model;
	
	public ArrayList<Class> classes;
	
	public ArrayList<Relation> relations;
	
	public ArrayList<Attribute> attributes;
	
	public XmiModel xmiModel;

	
	public CustomUpload(File f)
	{
		File dir = new File("C:/uploads/meta/");
		
		if(dir.isDirectory())
		{
			File[] metas =  dir.listFiles();
			if(metas!=null)
			{
				File recent = null;
				long mostRecentFile = 0; 
				for(File meta:metas)
				{					
					if(meta.lastModified() > mostRecentFile)
					{
						recent = meta;
						mostRecentFile = recent.lastModified();
					}
				}				
				
				reader = new ModelReader();
				reader.setMetamodelUri(recent.getAbsolutePath());
				readModel(f);
			}
		}
	}
	
	public void readModel(File f)
	{
		reader.setModelUri(f.getAbsolutePath());
		try {
			reader.darModeloEnDSL();
			reader.saveModel();
			model = reader.getModel();
			classes = reader.getClasses();
			attributes = reader.getAttributes();
			relations = reader.getRelations();
			xmiModel = reader.getXmiModel();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the model
	 */
	public Model getModel() {
		return model;
	}

	/**
	 * @return the classes
	 */
	public ArrayList<Class> getClasses() {
		return classes;
	}

	/**
	 * @return the relations
	 */
	public ArrayList<Relation> getRelations() {
		return relations;
	}

	/**
	 * @return the attributes
	 */
	public ArrayList<Attribute> getAttributes() {
		return attributes;
	}

	/**
	 * @return the xmiModel
	 */
	public XmiModel getXmiModel() {
		return xmiModel;
	}
	
	
	
	

}
